package com.foreverwin.jzymes.websocket;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.mapper.DashBoardAdviceMapper;
import com.foreverwin.jzymes.common.service.IActivityRuleAdviceService;
import com.foreverwin.jzymes.common.service.ICustomFieldsAdviceService;
import com.foreverwin.jzymes.common.service.IItemGroupAdviceService;
import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.util.CollectionUtil;
import com.foreverwin.jzymes.framework.annotation.WebSocketHandler;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.websocket.TextMessageWebSocketWraper;
import com.foreverwin.jzymes.production.gr.enums.GoodsReceiveTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebSocketHandler(channel = "Z_DB_PLAN_ACHIEVE")
public class PlanAchieveHandler extends TextMessageWebSocketWraper {

    @Autowired
    IActivityRuleAdviceService activityRuleAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    DashBoardAdviceMapper dashBoardAdviceMapper;

    private Map<String,String> activityRuleMap;

    @Override
    public void doAfterOpen(Connection connection) {
        activityRuleMap = activityRuleAdviceService.findActivityRules( getChannel() );
    }

    @Override
    public void doAfterClose() {

    }

    @Override
    public JSONObject doReceiveAndReply(String messageType, JSONObject requestParams) {
        if( "WS_INIT_DATA".equals( messageType ) ){
            return buildPlanArchieveData();
        }
        return null;
    }

    @Override
    public void onAppEvent(AppEvent<?> appEvent) {

    }

    public JSONObject buildPlanArchieveData(){
        String site = activityRuleMap.get( "SITE" );

        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        List<Map<String,Object>> itemMemberList = itemGroupAdviceService.findItemGroupMember( site, itemGroup );
        Long plantQty = 0L;
        if( itemMemberList.size() > 0 ){
            String customValue = customFieldsAdviceService.findCustomValue( (String) itemMemberList.get( 0 ).get( "ITEM_BO" ), Constants.ITEM_CUSTOM_FIELD_PLAN_QTY );
            if( StringUtils.isNotBlank( customValue ) ){
                plantQty = Long.valueOf( customValue );
            }
        }

        Date[] dayRange = productionShiftService.findProductionDayRange( site );
        List<Map<String,Object>> grList = dashBoardAdviceMapper.findGoodsReceiptByDateRange( site, GoodsReceiveTypeEnum.OK_PRODUCT.getValue(), dayRange[0], dayRange[1] );

        List<Map<String,Object>> details = new ArrayList<>();
        for( Map<String,Object> grMap : grList ){
            String itemLabel = customFieldsAdviceService.findCustomValue( (String) grMap.get( "ITEM_BO" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL );
            BigDecimal qty = (BigDecimal)grMap.get( "QTY" );

            JSONObject detail = new JSONObject();
            detail.put( "id", itemLabel );
            detail.put( "cmplQty", qty );
            if( plantQty != 0 ){
                detail.put( "cmplRate", String.format( "%.2f%%", qty.divide( BigDecimal.valueOf( plantQty ), 4, BigDecimal.ROUND_HALF_UP ).multiply( BigDecimal.valueOf( 100 ) ) ) );
            }else{
                detail.put( "cmplRate", "0%" );
            }
            details.add( detail );
        }
        CollectionUtil.sort( details, "id" );

        JSONObject response = new JSONObject();
        response.put( "planQty", plantQty );
        response.put( "details", details );
        return response;
    }

}
